#########################################################################
## Replication file for                                           
## "Structuring intra-party politics: a mixed-method study of ideological 
##    and hierarchical factions in parties"                 
##                                                    
## Authors: Ann-Kristin Kölln (ann-kristin.kolln@gu.se) and                                                  
##  		Jonathan Polk (jonathan.polk@svet.lu.se)
##              
## 28th June 2023
#########################################################################


# R version 4.3.0 (2023-04-21) -- "Already Tomorrow"; Platform: x86_64-apple-darwin20 (64-bit)

rm(list = ls())

# load data file 
dat3<-read.csv("PartyMembershipSurvey.csv", header =TRUE)


## install and load needed packages
install.packages(c("dplyr", "tidyverse", "psych", "tidyr", "rapportools", "ggplot2", "Rmisc", "grid", "gtable", "cowplot"))
library(dplyr)
library(tidyverse)
library(psych)
library(tidyr)
library(rapportools)
library(ggplot2)
library(Rmisc)
library(grid)
library(gtable)
library(cowplot)



### testing association between ideological dimensions

# individual-level, by party and with p-values
id.cor <- dat3 %>%
  split(.$partiabb) %>%  
  map(~corr.test(x = .x %>% select(iself, iself_c),
                 use = "complete.obs",
                 method = "pearson",
                 adjust = "none",
                 alpha = 0.05,
                 ci = TRUE, minlength = 5)
  )

id.cor


#### ANALYSIS BY PARTY (AFTER TRIANGULATION), LEFT-RIGHT ####



### Social Democrats


## K-means cluster analysis

#slice data and only use data for S
S19<-dat3[dat3$partiabb=="S",]

set.seed(3)

S19<-S19 %>% drop_na(iself)

#k-means clustering on LR position, creating clusters and inspecting them against the LR scale
km.outS<-kmeans(S19[,2], 3, nstart=50)
S19$clustersS<-km.outS$cluster

table(S19$clustersS, S19$iself)

# recoding clusters as "-1" for the left-most, "0" for the centrist, and "1" for the right-most cluster, converting into a factor
S19$clustersF[S19$clustersS == 1]<- 1
S19$clustersF[S19$clustersS == 2]<- 0
S19$clustersF[S19$clustersS == 3]<- -1

S19$clustersF<-as.factor(S19$clustersF)

#checking that everything works and removing initial cluster variable
table(S19$clustersF, S19$iself)
S19$clustersS<-NULL


## Generating input for Figure 4 and Table E2

#sizes and positions of clusters
prop.table(table(S19$clustersF))*100
km.outS$centers


## Generating input for Table 2

#testing relationship with hierarchical factions: Chi-square and lambda test
prop.table(table(S19$clustersF, S19$any_office),2)*100
tbl1<-table(S19$clustersF, S19$any_office)
test1<-chisq.test(tbl1)
test1

lambda.test(tbl1, direction = 0)



### Moderates

## K-means cluster analysis

#slice data and only use data for M
M19<-dat3[dat3$partiabb=="M",]

set.seed(3)

M19<-M19 %>% drop_na(iself)

#k-means clustering on LR position, creating clusters and inspecting them against the LR scale
km.outM<-kmeans(M19[,2], 2, nstart=50)
M19$clustersM<-km.outM$cluster

table(M19$clustersM, M19$iself)

# recoding clusters as "-1" for the left-most and "1" for the right-most cluster, converting into a factor
M19$clustersF[M19$clustersM == 1]<--1
M19$clustersF[M19$clustersM == 2]<-1

M19$clustersF<-as.factor(M19$clustersF)

#checking that everything works and removing initial cluster variable
table(M19$clustersF, M19$iself)
M19$clustersM<-NULL


## Generating input for Figure 4 and Table E2

#size and position of clusters
prop.table(table(M19$clustersF))*100
km.outM$centers


## Generating input for Table 2

#testing relationship with hierarchical factions: Chi-square and lambda test
prop.table(table(M19$clustersF, M19$any_office),2)*100
tbl1<-table(M19$clustersF, M19$any_office)
test1<-chisq.test(tbl1)
test1

lambda.test(tbl1, direction = 0)



### Green Party

## K-means cluster analysis

#slice data and only use data for Mp
Mp19<-dat3[dat3$partiabb=="Mp",]

set.seed(3)

Mp19<-Mp19 %>% drop_na(iself)

#k-means clustering on LR position, creating clusters and inspecting them against the LR scale
km.outMp<-kmeans(Mp19[,2], 2, nstart=50)
Mp19$clustersMp<-km.outMp$cluster

table(Mp19$clustersMp, Mp19$iself)

# recoding clusters as "-1" for the left-most and "1" for the right-most cluster, converting into a factor
Mp19$clustersF[Mp19$clustersMp == 1]<-1
Mp19$clustersF[Mp19$clustersMp == 2]<--1

Mp19$clustersF<-as.factor(Mp19$clustersF)

#checking that everything works and removing initial cluster variable
table(Mp19$clustersF, Mp19$iself)
Mp19$clustersMp<-NULL


## Generating input for Figure 4 and Table E2

#size and position of clusters
prop.table(table(Mp19$clustersF))*100
km.outMp$centers

## Generating input for Table 2

#testing relationship with hierarchical factions: Chi-square and lambda test
prop.table(table(Mp19$clustersF, Mp19$any_office),2)*100
tbl1<-table(Mp19$clustersF, Mp19$any_office)
test1<-chisq.test(tbl1)
test1

lambda.test(tbl1, direction = 0)



### Left Party

## K-means cluster analysis

#slice data and only use data for Mp
V19<-dat3[dat3$partiabb=="V",]

set.seed(3)

V19<-V19 %>% drop_na(iself)

#k-means clustering on LR position, creating clusters and inspecting them against the LR scale
km.outV<-kmeans(V19 [,2], 2, nstart=50)
V19$clustersV<-km.outV$cluster

table(V19$clustersV, V19$iself)

# recoding clusters as "-1" for the left-most and "1" for the right-most cluster, converting into a factor
V19$clustersF[V19$clustersV == 1]<--1
V19$clustersF[V19$clustersV == 2]<- 1

V19$clustersF<-as.factor(V19$clustersF)

#checking that everything works and removing initial cluster variable
table(V19$clustersF, V19$iself)
V19$clustersV<-NULL


## Generating input for Figure 4 and Table E2

#size and position of clusters
prop.table(table(V19$clustersF))*100
km.outV$centers

## Generating input for Table 2

#testing relationship with hierarchical factions: Chi-square and lambda test
prop.table(table(V19$clustersF, V19$any_office),2)*100
tbl1<-table(V19$clustersF, V19$any_office)
test1<-chisq.test(tbl1)
test1

lambda.test(tbl1, direction = 0)



### Christian Democrats

## K-means cluster analysis

#slice data and only use data for KD
KD19<-dat3[dat3$partiabb=="KD",]

set.seed(3)

KD19<-KD19 %>% drop_na(iself)

#k-means clustering on LR position, creating clusters and inspecting them against the LR scale
km.outKD<-kmeans(KD19 [,2], 2, nstart=50)
KD19$clustersKD<-km.outKD$cluster

table(KD19$clustersKD, KD19$iself)

# recoding clusters as "-1" for the left-most and "1" for the right-most cluster, converting into a factor
KD19$clustersF[KD19$clustersKD == 1]<--1
KD19$clustersF[KD19$clustersKD == 2]<-1

KD19$clustersF<-as.factor(KD19$clustersF)

#checking that everything works and removing initial cluster variable
table(KD19$clustersF, KD19$iself)
KD19$clustersKD<-NULL


## Generating input for Figure 4 and Table E2

#size and position of clusters
prop.table(table(KD19$clustersF))*100
km.outKD$centers


## Generating input for Table 2

#testing relationship with hierarchical factions: Chi-square and lambda test
prop.table(table(KD19$clustersF, KD19$any_office),2)*100
tbl1<-table(KD19$clustersF, KD19$any_office)
test1<-chisq.test(tbl1)
test1

lambda.test(tbl1, direction = 0)



### Liberals

## K-means cluster analysis

#slice data and only use data for L
L19<-dat3[dat3$partiabb=="L",]

set.seed(3)

L19<-L19 %>% drop_na(iself)

#k-means clustering on LR position, creating clusters and inspecting them against the LR scale
km.outFp<-kmeans(L19[,2], 3, nstart=50)
L19$clustersFp<-km.outFp$cluster

table(L19$clustersFp, L19$iself)

# recoding clusters as "-1" for the left-most, "0" for the centrist, and "1" for the right-most cluster, converting into a factor
L19$clustersF[L19$clustersFp == 1]<-0
L19$clustersF[L19$clustersFp == 2]<-1
L19$clustersF[L19$clustersFp == 3]<--1

L19$clustersF<-as.factor(L19$clustersF)

#checking that everything works and removing initial cluster variable
table(L19$clustersF, L19$iself)
L19$clustersFp<-NULL


## Generating input for Figure 4 and Table E2

#size and position of clusters
prop.table(table(L19$clustersF))*100
km.outFp$centers


## Generating input for Table 2

#testing relationship with hierarchical factions: Chi-square and lambda test
prop.table(table(L19$clustersF, L19$any_office),2)*100
tbl1<-table(L19$clustersF, L19$any_office)
test1<-chisq.test(tbl1)
test1

lambda.test(tbl1, direction = 0)



### Center Party

## K-means cluster analysis

#slice data and only use data for C
C19<-dat3[dat3$partiabb=="C",]

set.seed(3)

C19<-C19 %>% drop_na(iself)

#k-means clustering on LR position, creating clusters and inspecting them against the LR scale
km.outC<-kmeans(C19[,2], 2, nstart=50)
C19$clustersC<-km.outC$cluster

table(C19$clustersC, C19$iself)

# recoding clusters as "-1" for the left-most  and "1" for the right-most cluster, converting into a factor
C19$clustersF[C19$clustersC == 1]<--1
C19$clustersF[C19$clustersC == 2]<-1

C19$clustersF<-as.factor(C19$clustersF)

#checking that everything works and removing initial cluster variable
table(C19$clustersF, C19$iself)
C19$clustersC<-NULL


## Generating input for Figure 4 and Table E2

#size and position of clusters
prop.table(table(C19$clustersF))*100
km.outC$centers

## Generating input for Table 2

#testing relationship with hierarchical factions: Chi-square and lambda test
prop.table(table(C19$clustersF, C19$any_office),2)*100
tbl1<-table(C19$clustersF, C19$any_office)
test1<-chisq.test(tbl1)
test1

lambda.test (tbl1, direction = 0)




### EXPLORING THE CONTENT OF FACTIONAL DIVIDES ALONG LEFT-RIGHT DIMNESION ###


## Preparing data for Figure 2

# merge party-specific data again into one

dat4<-rbind(C19, L19, KD19, V19, Mp19, M19, S19)

#rename cluster variable and remove old one
dat4$clustersF_LR<-dat4$clustersF
dat4<-dat4[,-16]

# relabel cluster variable and assign as factor
dat4$FactionLR[dat4$clustersF == "-1"]<-"Left"
dat4$FactionLR[dat4$clustersF == "1"]<-"Right"
dat4$FactionLR[dat4$clustersF == "0"]<-"Center"

dat4$FactionLR<-factor(dat4$FactionLR, levels = c("Left", "Center", "Right"))


#summarize the three individual positional issues, by faction and party
faction_means_issue1<-summarySE(dat4, measurevar ="reduce_income", groupvars= c("FactionLR", "party2"), na.rm=TRUE)
faction_means_issue2<-summarySE(dat4, measurevar ="private_health", groupvars= c("FactionLR", "party2"), na.rm=TRUE)
faction_means_issue3<-summarySE(dat4, measurevar ="intervene_economy", groupvars= c("FactionLR", "party2"), na.rm=TRUE)


# merge all three summaries into one dataset
faction_means_issue1$issue<-c("income")
faction_means_issue2$issue<-c("health")
faction_means_issue3$issue<-c("economy")

colnames(faction_means_issue1)<- c("FactionLR", "party2", "N", "mean", "sd", "se", "ci", "issue")
colnames(faction_means_issue2)<- c("FactionLR", "party2", "N", "mean", "sd", "se", "ci", "issue")
colnames(faction_means_issue3)<- c("FactionLR", "party2", "N", "mean", "sd", "se", "ci", "issue")

faction_means_issuesLR<-rbind(faction_means_issue1, faction_means_issue2, faction_means_issue3)

#export dataset to create input for Table E3
write.csv(faction_means_issuesLR, file = "content_faction_LR.csv")


#write function to move legend in Figure 2 into empty facets
shift_legend <- function(p){

  # check if p is a valid object
  if(!"gtable" %in% class(p)){
    if("ggplot" %in% class(p)){
      gp <- ggplotGrob(p) # convert to grob
    } else {
      message("This is neither a ggplot object nor a grob generated from ggplotGrob. Returning original plot.")
      return(p)
    }
  } else {
    gp <- p
  }

  # check for unfilled facet panels
  facet.panels <- grep("^panel", gp[["layout"]][["name"]])
  empty.facet.panels <- sapply(facet.panels, function(i) "zeroGrob" %in% class(gp[["grobs"]][[i]]))
  empty.facet.panels <- facet.panels[empty.facet.panels]
  if(length(empty.facet.panels) == 0){
    message("There are no unfilled facet panels to shift legend into. Returning original plot.")
    return(p)
  }
  
# establish extent of unfilled facet panels (including any axis cells in between)
  empty.facet.panels <- gp[["layout"]][empty.facet.panels, ]
  empty.facet.panels <- list(min(empty.facet.panels[["t"]]), min(empty.facet.panels[["l"]]),
                             max(empty.facet.panels[["b"]]), max(empty.facet.panels[["r"]]))
  names(empty.facet.panels) <- c("t", "l", "b", "r")

  # extract legend & copy over to location of unfilled facet panels
  guide.grob <- which(gp[["layout"]][["name"]] == "guide-box")
  if(length(guide.grob) == 0){
    message("There is no legend present. Returning original plot.")
    return(p)
  }
  gp <- gtable_add_grob(x = gp,
                        grobs = gp[["grobs"]][[guide.grob]],
                        t = empty.facet.panels[["t"]],
                        l = empty.facet.panels[["l"]],
                        b = empty.facet.panels[["b"]],
                        r = empty.facet.panels[["r"]],
                        name = "new-guide-box")

  # squash the original guide box's row / column (whichever applicable)
  # & empty its cell
  guide.grob <- gp[["layout"]][guide.grob, ]
  if(guide.grob[["l"]] == guide.grob[["r"]]){
    gp <- gtable_squash_cols(gp, cols = guide.grob[["l"]])
  }
  if(guide.grob[["t"]] == guide.grob[["b"]]){
    gp <- gtable_squash_rows(gp, rows = guide.grob[["t"]])
  }
  gp <- gtable_remove_grobs(gp, "guide-box")

  return(gp)
}


# generating Figure 2
factions_issuesLR<- ggplot(faction_means_issuesLR, aes(x=FactionLR, y= mean, color = issue)) +
				geom_errorbar(aes(ymin= mean-sd, ymax= mean +sd), width=.1, position = position_dodge(width =0.5)) + 
    			geom_point(size=2, position = position_dodge(width =0.5))+
    			geom_line ()+
    			xlab("Factions, left-right scale")+
   				ylab(" Support (1-5) means and standard deviations")+ 
				theme_bw() +
				facet_wrap(~party2)+
				scale_colour_grey(labels = c("abstain economic intervention", "healthcare privatization", "reduce income inequalities"))
				
			
jpeg("Figure 2.jpeg", width= 8, height= 6, units='in', res=300)        
grid.draw(shift_legend(factions_issuesLR))	
dev.off()



## check whether group means are statistically significantly different: additional input for Table E3

#split files per party
C_issueLR<-dat4[dat4$partiabb =="C",]
KD_issueLR<-dat4[dat4$partiabb =="KD",]
Mp_issueLR<-dat4[dat4$partiabb =="Mp",]
V_issueLR<-dat4[dat4$partiabb =="V",]
L_issueLR<-dat4[dat4$partiabb =="L",]
M_issueLR<-dat4[dat4$partiabb =="M",]
S_issueLR<-dat4[dat4$partiabb =="S",]

#income
C_LR<- aov(reduce_income ~ factor(FactionLR), data= C_issueLR)
TukeyHSD(C_LR) 

KD_LR<- aov(reduce_income ~ factor(FactionLR), data= KD_issueLR)
TukeyHSD(KD_LR)

Mp_LR<- aov(reduce_income ~ factor(FactionLR), data= Mp_issueLR)
TukeyHSD(Mp_LR)

V_LR<- aov(reduce_income ~ factor(FactionLR), data= V_issueLR) 
TukeyHSD(V_LR)

L_LR<- aov(reduce_income ~ factor(FactionLR), data= L_issueLR) 
TukeyHSD(L_LR)

M_LR<- aov(reduce_income ~ factor(FactionLR), data= M_issueLR) 
TukeyHSD(M_LR)

S_LR<- aov(reduce_income ~ factor(FactionLR), data= S_issueLR) 
TukeyHSD(S_LR)


#health
C_LR2<- aov(private_health ~ factor(FactionLR), data= C_issueLR)
TukeyHSD(C_LR2) 

KD_LR2<- aov(private_health ~ factor(FactionLR), data= KD_issueLR) 
TukeyHSD(KD_LR2)

Mp_LR2<- aov(private_health ~ factor(FactionLR), data= Mp_issueLR) 
TukeyHSD(Mp_LR2)

V_LR2<- aov(private_health ~ factor(FactionLR), data= V_issueLR) 
TukeyHSD(V_LR2) 

L_LR2<- aov(private_health ~ factor(FactionLR), data= L_issueLR) 
TukeyHSD(L_LR2)

M_LR2<- aov(private_health ~ factor(FactionLR), data= M_issueLR) 
TukeyHSD(M_LR2) 

S_LR2<- aov(private_health ~ factor(FactionLR), data= S_issueLR) 
TukeyHSD(S_LR2) 


#economy
C_LR3<- aov(intervene_economy ~ factor(FactionLR), data= C_issueLR)
TukeyHSD(C_LR3)

KD_LR3<- aov(intervene_economy ~ factor(FactionLR), data= KD_issueLR) 
TukeyHSD(KD_LR3) 

Mp_LR3<- aov(intervene_economy ~ factor(FactionLR), data= Mp_issueLR) 
TukeyHSD(Mp_LR3)

V_LR3<- aov(intervene_economy ~ factor(FactionLR), data= V_issueLR)
TukeyHSD(V_LR3) 

L_LR3<- aov(intervene_economy ~ factor(FactionLR), data= L_issueLR)
TukeyHSD(L_LR3)

M_LR3<- aov(intervene_economy ~ factor(FactionLR), data= M_issueLR) 
TukeyHSD(M_LR3) 

S_LR3<- aov(intervene_economy ~ factor(FactionLR), data= S_issueLR) 
TukeyHSD(S_LR3)




#### ANALYSIS BY PARTY (AFTER TRIANGULATION), GAL-TAN ####


### Social Democrats


## K-means cluster analysis

#slice data and only use data for S
S19<-dat4[dat4$partiabb=="S",]

set.seed(4)

S19<-S19 %>% drop_na(iself_c)

#k-means clustering on GT position, creating clusters and inspecting them against the GT scale
km.outS<-kmeans(S19[,3], 2, nstart=50)
S19$clustersS<-km.outS$cluster

table(S19$clustersS, S19$iself_c)

# recoding clusters as "-1" for the gal-most and "1" for the tan-most cluster, converting into a factor
S19$clustersF[S19$clustersS == 1]<--1
S19$clustersF[S19$clustersS == 2]<- 1

S19$clustersF<-as.factor(S19$clustersF)

#checking that everything works
table(S19$clustersF, S19$iself_c)


## Generating input for Table 3

#testing relationship with hierarchical factions: Chi-square and lambda
prop.table(table(S19 $clustersS, S19 $any_office),2)*100
tbl1<-table(S19$clustersS, S19$any_office)
test1<-chisq.test(tbl1)
test1

lambda.test(tbl1, direction = 0)


# remove initial cluster variable
S19$clustersS<-NULL


## Generating input for Figure 5 and Table E2

#size and position of clusters
prop.table(table(S19$clustersF))*100
km.outS$centers




### Moderates

## K-means cluster analysis

#slice data and only use data for M
M19<-dat4[dat4$partiabb=="M",]

set.seed(4)

M19<-M19 %>% drop_na(iself_c)

#k-means clustering on GT position, creating clusters and inspecting them against the GT scale
km.outM<-kmeans(M19[,3], 2, nstart=50)
M19$clustersM<-km.outM$cluster

table(M19$clustersM, M19$iself_c)

# recoding clusters as "-1" for the gal-most and "1" for the tan-most cluster, converting into a factor
M19$clustersF[M19$clustersM == 1]<--1
M19$clustersF[M19$clustersM == 2]<-1

M19$clustersF<-as.factor(M19$clustersF)

#checking that everything works
table(M19$clustersF, M19$iself_c)


## Generating input for Table 3

#testing relationship with hierarchical factions: Chi-square and lambda
prop.table(table(M19 $clustersM, M19 $any_office),2)*100
tbl1<-table(M19$clustersM, M19$any_office)
test1<-chisq.test(tbl1)
test1

lambda.test(tbl1, direction = 0)

# remove initial cluster variable
M19$clustersM<-NULL


## Generating input for Figure 5 and Table E2

#size and position of clusters
prop.table(table(M19$clustersF))*100
km.outM$centers




### Green Party

## K-means cluster analysis

#slice data and only use data for Mp
Mp19<-dat4[dat4$partiabb=="Mp",]

set.seed(4)

Mp19<-Mp19 %>% drop_na(iself_c)

#k-means clustering on GT position, creating clusters and inspecting them against the GT scale
km.outMp<-kmeans(Mp19[,3], 3, nstart=50)
Mp19$clustersMp<-km.outMp$cluster

table(Mp19$clustersMp, Mp19$iself_c)

# recoding clusters as "-1" for the gal-most, "0" for centrist cluster, and "1" for the tan-most cluster, converting into a factor
Mp19$clustersF[Mp19$clustersMp == 1]<-1
Mp19$clustersF[Mp19$clustersMp == 2]<-0
Mp19$clustersF[Mp19$clustersMp == 3]<--1

Mp19$clustersF<-as.factor(Mp19$clustersF)

#checking that everything works
table(Mp19$clustersF, Mp19$iself_c)


## Generating input for Table 3

#testing relationship with hierarchical factions: Chi-square and lambda
prop.table(table(Mp19 $clustersMp, Mp19 $any_office),2)*100
tbl1<-table(Mp19$clustersMp, Mp19$any_office)
test1<-chisq.test(tbl1)
test1

lambda.test(tbl1, direction = 0)

# remove initial cluster variable
Mp19$clustersMp<-NULL


## Generating input for Figure 5 and Table E2

#size and position of clusters
prop.table(table(Mp19$clustersF))*100
km.outMp$centers



### Left Party

## K-means cluster analysis

#slice data and only use data for V
V19<-dat4[dat4$partiabb=="V",]

set.seed(4)

V19<-V19 %>% drop_na(iself_c)

#k-means clustering on GT position, creating clusters and inspecting them against the GT scale
km.outV<-kmeans(V19 [,3], 2, nstart=50)
V19$clustersV<-km.outV$cluster

table(V19$clustersV, V19$iself_c)

# recoding clusters as "-1" for the gal-most and "1" for the tan-most cluster, converting into a factor
V19$clustersF[V19$clustersV == 1]<--1
V19$clustersF[V19$clustersV == 2]<-1

V19$clustersF<-as.factor(V19$clustersF)

#checking that everything works
table(V19$clustersF, V19$iself_c)



## Generating input for Table 3

#testing relationship with hierarchical factions: Chi-square and lambda
prop.table(table(V19$clustersV, V19$any_office),2)*100
tbl1<-table(V19$clustersV, V19$any_office)
test1<-chisq.test(tbl1)
test1

lambda.test(tbl1, direction = 0)

# remove initial cluster variable
V19$clustersV<-NULL


## Generating input for Figure 5 and Table E2

#size and position of clusters
prop.table(table(V19$clustersF))*100
km.outV$centers





### Christian Democrats


## K-means cluster analysis

#slice data and only use data for KD
KD19<-dat4[dat4$partiabb=="KD",]

set.seed(4)

KD19<-KD19 %>% drop_na(iself_c)

#k-means clustering on GT position, creating clusters and inspecting them against the GT scale
km.outKD<-kmeans(KD19 [,3], 2, nstart=50)
KD19$clustersKD<-km.outKD$cluster

table(KD19$clustersKD, KD19$iself_c)


# recoding clusters as "-1" for the gal-most and "1" for the tan-most cluster, converting into a factor
KD19$clustersF[KD19$clustersKD == 1]<--1
KD19$clustersF[KD19$clustersKD == 2]<-1

KD19$clustersF<-as.factor(KD19$clustersF)


#checking that everything works
table(KD19$clustersF, KD19$iself_c)


## Generating input for Table 3

#testing relationship with hierarchical factions: Chi-square and lambda
prop.table(table(KD19$clustersKD, KD19$any_office),2)*100
tbl1<-table(KD19$clustersKD, KD19$any_office)
test1<-chisq.test(tbl1)
test1

lambda.test(tbl1, direction = 0) 


# remove initial cluster variable
KD19$clustersKD<-NULL


## Generating input for Figure 5 and Table E2

#size and position of clusters
prop.table(table(KD19$clustersF))*100
km.outKD$centers





### Liberals

## K-means cluster analysis

#slice data and only use data for L
L19<-dat4[dat4$partiabb=="L",]

set.seed(4)

L19<-L19 %>% drop_na(iself_c)

#k-means clustering on GT position, creating clusters and inspecting them against the GT scale
km.outFp<-kmeans(L19[,3], 3, nstart=50)
L19$clustersFp<-km.outFp$cluster

table(L19$clustersFp, L19$iself_c)

# recoding clusters as "-1" for the gal-most, "0" for the centrist cluster, and "1" for the tan-most cluster, converting into a factor
L19$clustersF[L19$clustersFp == 1]<-1
L19$clustersF[L19$clustersFp == 2]<-0
L19$clustersF[L19$clustersFp == 3]<--1

L19$clustersF<-as.factor(L19$clustersF)

#checking that everything works
table(L19$clustersF, L19$iself_c)


## Generating input for Table 3

#testing relationship with hierarchical factions: Chi-square and lambda
prop.table(table(L19$clustersFp, L19$any_office),2)*100
tbl1<-table(L19$clustersFp, L19$any_office)
test1<-chisq.test(tbl1)
test1

lambda.test(tbl1, direction = 0)

# remove initial cluster variable
L19$clustersFp<-NULL


## Generating input for Figure 5 and Table E2

#size and position of clusters
prop.table(table(L19$clustersF))*100
km.outFp$centers




### Center Party

## K-means cluster analysis

#slice data and only use data for C
C19<-dat4[dat4$partiabb=="C",]

set.seed(4)

C19<-C19 %>% drop_na(iself_c)

#k-means clustering on GT position, creating clusters and inspecting them against the GT scale
km.outC<-kmeans(C19[,3], 1, nstart=50)
C19$clustersC<-km.outC$cluster

table(C19$clustersC, C19$iself_c)

# recoding "0" for the centrist cluster, converting into a factor
C19$clustersF[C19$clustersC == 1]<-0

C19$clustersF<-as.factor(C19$clustersF)

#checking that everything works
table(C19$clustersF, C19$iself_c)

# remove initial cluster variable
C19$clustersC<-NULL


## Generating input for Figure 5 and Table E2

#size and position of clusters
prop.table(table(C19$clustersF))*100
km.outC$centers




### EXPLORING THE CONTENT OF FACTIONAL DIVIDES ALONG LEFT-RIGHT DIMNESION ###


## Preparing data for Figure 3

# merge party-specific data again into one

dat5<-rbind(C19, L19, KD19, V19, Mp19, S19, M19)

#rename clustering variable and remove old one
dat5$clustersF_GT<-dat5$clustersF
dat5<-dat5[,-18]


# relabel cluster variable and assign as factor
dat5 $FactionGT[dat5 $clustersF_GT == "-1"]<-"GAL"
dat5 $FactionGT[dat5 $clustersF_GT == "1"]<-"TAN"
dat5 $FactionGT[dat5 $clustersF_GT == "0"]<-"Center"

dat5 $FactionGT<-factor(dat5 $FactionGT, levels = c("GAL", "Center", "TAN"))


#summarize the three individual positional issues, by faction and party
faction_means_issue7<-summarySE(dat5, measurevar ="immigrants_adapt", groupvars= c("FactionGT", "party2"), na.rm=TRUE)
faction_means_issue8<-summarySE(dat5, measurevar ="fewer_refugees", groupvars= c("FactionGT", "party2"), na.rm=TRUE)
faction_means_issue9<-summarySE(dat5, measurevar ="stiffer_sentences", groupvars= c("FactionGT", "party2"), na.rm=TRUE)


# merge all three summaries into one dataset
faction_means_issue7$issue<-c("assimilation")
faction_means_issue8$issue<-c("refugees")
faction_means_issue9$issue<-c("sentences")

colnames(faction_means_issue7)<- c("FactionGT", "party2", "N", "mean", "sd", "se", "ci", "issue")
colnames(faction_means_issue8)<- c("FactionGT", "party2", "N", "mean", "sd", "se", "ci", "issue")
colnames(faction_means_issue9)<- c("FactionGT", "party2", "N", "mean", "sd", "se", "ci", "issue")

faction_means_issuesGT<-rbind(faction_means_issue7, faction_means_issue8, faction_means_issue9)

#export dataset to create input for Table E3
write.csv(faction_means_issuesGT, file = "content_faction_GT.csv")



#generate Figure 3
factions_issuesGT<- ggplot(faction_means_issuesGT, aes(x=FactionGT, y= mean, color = issue)) +
				geom_errorbar(aes(ymin= mean-sd, ymax= mean +sd), width=.1, position = position_dodge(width =0.5)) + 
    			geom_point(size=2, position = position_dodge(width =0.5))+
    			geom_line ()+
    			xlab("Factions, GAL-TAN scale")+
   				ylab(" Support (1-5) means and standard deviations")+ 
				theme_bw() +
				facet_wrap(~party2)+
				scale_colour_grey(labels = c("assimilation immigrants", "fewer refugees", "stiffer sentences"))

jpeg("Figure 3.jpeg", width= 8, height= 6, units='in', res=300)        
grid.draw(shift_legend(factions_issuesGT))
dev.off()



## check whether group means are statistically significantly different

#split files per party
C_issueGT<-dat5[dat5$partiabb =="C",]
KD_issueGT<-dat5[dat5$partiabb =="KD",]
Mp_issueGT<-dat5[dat5$partiabb =="Mp",]
V_issueGT<-dat5[dat5$partiabb =="V",]
L_issueGT<-dat5[dat5$partiabb =="L",]
M_issueGT<-dat5[dat5$partiabb =="M",]
S_issueGT<-dat5[dat5$partiabb =="S",]


# immigrants
KD_GT<- aov(immigrants_adapt ~ factor(FactionGT), data= KD_issueGT) 
TukeyHSD(KD_GT)

Mp_GT<- aov(immigrants_adapt ~ factor(FactionGT), data= Mp_issueGT) 
TukeyHSD(Mp_GT)

V_GT<- aov(immigrants_adapt ~ factor(FactionGT), data= V_issueGT) 
TukeyHSD(V_GT)

L_GT<- aov(immigrants_adapt ~ factor(FactionGT), data= L_issueGT) 
TukeyHSD(L_GT)

M_GT<- aov(immigrants_adapt ~ factor(FactionGT), data= M_issueGT) 
TukeyHSD(M_GT)

S_GT<- aov(immigrants_adapt ~ factor(FactionGT), data= S_issueGT)
TukeyHSD(S_GT)


# refugees
KD_GT2<- aov(fewer_refugees ~ factor(FactionGT), data= KD_issueGT) 
TukeyHSD(KD_GT2)

Mp_GT2<- aov(fewer_refugees ~ factor(FactionGT), data= Mp_issueGT) 
TukeyHSD(Mp_GT2)

V_GT2<- aov(fewer_refugees ~ factor(FactionGT), data= V_issueGT) 
TukeyHSD(V_GT2)

L_GT2<- aov(fewer_refugees ~ factor(FactionGT), data= L_issueGT) 
TukeyHSD(L_GT2)

M_GT2<- aov(fewer_refugees ~ factor(FactionGT), data= M_issueGT) 
TukeyHSD(M_GT2)

S_GT2<- aov(fewer_refugees ~ factor(FactionGT), data= S_issueGT) 
TukeyHSD(S_GT2)


# sentences
KD_GT3<- aov(stiffer_sentences ~ factor(FactionGT), data= KD_issueGT) 
TukeyHSD(KD_GT3)

Mp_GT3<- aov(stiffer_sentences ~ factor(FactionGT), data= Mp_issueGT) 
TukeyHSD(Mp_GT3)

V_GT3<- aov(stiffer_sentences ~ factor(FactionGT), data= V_issueGT)
TukeyHSD(V_GT3)

L_GT3<- aov(stiffer_sentences ~ factor(FactionGT), data= L_issueGT) 
TukeyHSD(L_GT3) 

M_GT3<- aov(stiffer_sentences ~ factor(FactionGT), data= M_issueGT)
TukeyHSD(M_GT3)

S_GT3<- aov(stiffer_sentences ~ factor(FactionGT), data= S_issueGT) 
TukeyHSD(S_GT3)


#### ADDITIONAL ANALYSES REPORTED IN THE APPENDIX ####

### Appendix E


### Association between ideological factions within parties

## split file by party
S19_3<-dat5[dat5$partiabb == "S",]
V19_3<-dat5[dat5$partiabb == "V",]
KD19_3<-dat5[dat5$partiabb == "KD",]
M19_3<-dat5[dat5$partiabb == "M",]
Mp19_3<-dat5[dat5$partiabb == "Mp",]
C19_3<-dat5[dat5$partiabb == "C",]
L19_3<-dat5[dat5$partiabb == "L",]


## Generating input for Table E1, by party: Lambda

#Social Democrats
S19_3$clustersF_GT<-droplevels(S19_3$clustersF_GT)
S19_3$clustersF_LR<-droplevels(S19_3$clustersF_LR)

prop.table(table(S19_3$clustersF_GT, S19_3 $clustersF_LR),2)*100

lambda.test(tbl1, direction = 0) # row = 0; column = 0.9


#Left Party
V19_3$clustersF_GT<-droplevels(V19_3$clustersF_GT)
V19_3$clustersF_LR<-droplevels(V19_3$clustersF_LR)

prop.table(table(V19_3$clustersF_GT, V19_3 $clustersF_LR),2)*100
tbl1<-table(V19_3 $clustersF_GT, V19_3 $clustersF_LR)

lambda.test(tbl1, direction = 0)


#Christian Democrats
KD19_3$clustersF_GT<-droplevels(KD19_3$clustersF_GT)
KD19_3$clustersF_LR<-droplevels(KD19_3$clustersF_LR)

prop.table(table(KD19_3 $clustersF_GT, KD19_3 $clustersF_LR),2)*100
tbl1<-table(KD19_3 $clustersF_GT, KD19_3 $clustersF_LR)
lambda.test(tbl1, direction = 0)


#Moderates
M19_3$clustersF_GT<-droplevels(M19_3 $clustersF_GT)
M19_3 $clustersF_LR<-droplevels(M19_3 $clustersF_LR)

prop.table(table(M19_3 $clustersF_GT, M19_3 $clustersF_LR),2)*100
tbl1<-table(M19_3 $clustersF_GT, M19_3 $clustersF_LR)
lambda.test(tbl1, direction = 0)


#Green Party
Mp19_3$clustersF_GT<-droplevels(Mp19_3 $clustersF_GT)
Mp19_3 $clustersF_LR<-droplevels(Mp19_3 $clustersF_LR)

prop.table(table(Mp19_3 $clustersF_GT, Mp19_3 $clustersF_LR),2)*100
tbl1<-table(Mp19_3 $clustersF_GT, Mp19_3 $clustersF_LR)
lambda.test(tbl1, direction = 0)


#Liberals
L19_3$clustersF_GT<-droplevels(L19_3 $clustersF_GT)
L19_3 $clustersF_LR<-droplevels(L19_3 $clustersF_LR)

prop.table(table(L19_3 $clustersF_GT, L19_3 $clustersF_LR),2)*100
tbl1<-table(L19_3 $clustersF_GT, L19_3 $clustersF_LR)
lambda.test(tbl1, direction = 0)